package com.jye.rseven.user.serivce.impl;

import org.springframework.stereotype.Service;
import com.jye.rseven.Constants.MyConstants;
import com.jye.rseven.domain.ChargeRecord;
import com.jye.rseven.domain.User;
import com.jye.rseven.service.ChargeRecordService;
import com.jye.rseven.user.mapper.ChargeRecordMapper;
import com.jye.rseven.user.mapper.PaymentMapper;
import com.jye.rseven.user.mapper.PromotionMapper;
import com.jye.rseven.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ChargeRecordServiceImpl implements ChargeRecordService {

    @Autowired
    ChargeRecordMapper chargeRecordMapper;

    @Autowired
    PromotionMapper promotionMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    PaymentMapper paymentMapper;

    @Override
    public Integer findChargeRecordByuId(int uId) {
         String status=MyConstants.chargeRecordStatus.success;
        return chargeRecordMapper.findChargeRecordByuId(uId,status);
    }

    @Override
    public List<ChargeRecord> findRecordByuId(int uId) {

        List<ChargeRecord> list=chargeRecordMapper.findRecordByuId(uId);

        return list;
    }

    @Override
    public List<User> findMyChargeRecord(int uId) {
        List<Integer> ids = chargeRecordMapper.findPayIdByuId(uId);
        List<Integer> uidList=new ArrayList<>();
        for (Integer pId : ids) {
            Integer UId=promotionMapper.findUserIdBypId(pId);
            uidList.add(UId);
        }
        List<User> userList=new ArrayList<>();
        for (Integer id : uidList) {
            User user = userMapper.findUserByuId(id);
            userList.add(user);
        }
        return  userList;
    }

    @Override
    public String  addChargeRecord(ChargeRecord chargeRecord) {
        try{
            String payStatus=paymentMapper.findPayStatusById(chargeRecord.getPId());
            String a=MyConstants.payStatus.paid;
            String b=MyConstants.payStatus.unpaid;
         //   System.out.println(payStatus);
          //  System.out.println(a);
          //  System.out.println(payStatus.equals(a));

            if(payStatus.equals(a)) {
                String status = MyConstants.chargeRecordStatus.success;
                chargeRecord.setStatus(status);
                //佣金,每推广成功一个人50元佣金
                chargeRecord.setCharges(50);
                //创建日期
                chargeRecord.setCreateTime(new Date());
                chargeRecordMapper.addChargeRecord(chargeRecord);
                return "推广成功";
            }else if(payStatus.equals(b)){
                String status = MyConstants.chargeRecordStatus.fail;
                chargeRecord.setStatus(status);
                //佣金,每推广成功一个人50元佣金
                chargeRecord.setCharges(50);
                //创建日期
                chargeRecord.setCreateTime(new Date());
                chargeRecordMapper.addChargeRecord(chargeRecord);
                return "推广失败";
            }else{
                return "无此订单";
            }
        }catch (Exception e){
            return "失败!";
        }
    }

    @Override
    public int updateChargeRecord(ChargeRecord chargeRecord) {
        try{
            chargeRecordMapper.updateChargeRecord(chargeRecord);
            return 1;
        }catch (Exception e){
            return 0;
        }
    }

    //只查询用户的有效总佣金
    @Override
    public int findChargesByuId(int uId) {
     //   String status=chargeRecordMapper.findChargeRecordStatusByuId(uId);
        String status=MyConstants.chargeRecordStatus.success;
        return chargeRecordMapper.findCharges(uId,status);
    }

}
